package controller

import (
	com "gostore/common"
	"gostore/dao/store"
	"gostore/model"
	"gostore/router"
	"strconv"

	//"strings"

	"github.com/gin-gonic/gin"
)

type OrderControllerT struct {
	BaseControllerT
}

var OrderController = OrderControllerT{}

type OrderPriceStruct struct {
	PayPrice        float64 `json:"pay_price"`         //支付金额
	RefundPrice     float64 `json:"refund_price"`      //退款金额
	PayPriceWx      float64 `json:"pay_price_wx"`      //微信支付金额
	PayPriceYue     float64 `json:"pay_price_yue"`     //余额支付金额
	PayPriceOffline float64 `json:"pay_price_offline"` //线下支付金额
	PayPriceOther   float64 `json:"pay_price_other"`   //其他支付金额
	UseIntegral     float64 `json:"use_integral"`      //用户使用积分
	BackIntegral    float64 `json:"back_integral"`     //退积分总数
	DeductionPrice  float64 `json:"deduction_price"`   //抵扣金额
	TotalNum        int     `json:"total_num"`         //商品总数
	CountSum        int     `json:"count_sum"`         //商品总数
	Brokerage       float64 `json:"brokerage"`         //背景色
	PayPostage      float64 `json:"pay_postage"`       //
}

// @Summary 订单信息
// @Description  通过id获取对应订单信息
// @Accept json
// @Produce json
// @Param id path int true "id"
// @Success 200 {string} string "{"code"":0,"data":"","msg":"ok"}"
// @Router /api/order/{id} [get]
func (o OrderControllerT) Info(c *gin.Context) {

	order := model.StoreOrder{}
	idstr := c.Query("id")
	id, err := strconv.Atoi(idstr)
	if err != nil {
		com.Err(c, 503, "错误的id")
		return
	}

	// dao.Order.xxxx
	err = store.OrderDao.GetById(&order, id)
	if err != nil {
		c.AbortWithError(503, err)
		return
	}

	com.Ok(c, order)
}

func (o OrderControllerT) List(c *gin.Context) {

	page, err1 := strconv.Atoi(c.Query("page"))
	if err1 != nil {
		page = 1
	}

	limit, err2 := strconv.Atoi(c.Query("limit"))
	if err2 != nil {
		limit = 6
	}

	orders := []model.StoreOrder{}
	count, err := store.OrderDao.List(&orders, page, limit)
	if err != nil {
		com.Err(c, 1, err.Error())
		return
	}
	mdata := make(map[string]interface{}, 0)
	mdata["list"] = orders
	mdata["count"] = count

	com.Ok(c, orders)
}

func (o OrderControllerT) GetBadge(c *gin.Context) {
	//没传参的话  status就是-9

	// s:= model.StoreOrder{Status:-9}
	// data, _ := s.GetWhereData("", 0)

	// total := OrderPriceStruct{
	// 	CountSum:len(data),
	// }

	// ids := ""
	// for _, val := range data {
	// 	ids += strconv.Itoa(val.Uid) + ","
	// 	total.TotalNum += val.TotalNum
	// 	total.PayPrice += val.PayPrice
	// 	total.PayPostage += val.PayPostage
	// 	total.UseIntegral+= val.UseIntegral
	// 	total.BackIntegral+= val.BackIntegral
	// 	total.DeductionPrice += val.DeductionPrice
	// 	total.RefundPrice += val.RefundPrice
	// }

	// if strings.Contains(ids, ",") {
	// 	ids = string([]rune(ids)[:len(ids) - 1])
	// }

	//UBill := []model.UserBill{}
	//_,err := store.OrderDao.QueryList(&UBill,0, 6, "category:now_money,type:brokerage,link_id:in:("+ids+")", "")
	//if err != nil {
	//	com.Err(c, 1, err.Error())
	//	return
	//}
	//
	//for _,val := range UBill{
	//	total.Brokerage += val.Number
	//}

	//此处用group
	//if val.PayType == "weixin" {
	//	total.PayPriceWx += val.PayPrice
	//} else if val.PayType == "yue" {
	//	total.PayPriceYue += val.PayPrice
	//} else if val.PayType == "offline" {
	//	total.PayPriceOffline += val.PayPrice
	//} else {
	//	total.PayPriceOther += val.PayPrice
	//}
	//com.Ok(c, total)
}

func (o OrderControllerT) AddNewRoutes() {
	router.AddRouteGroup("api/order/", []router.Route{
		router.Route{
			Method:  router.MGet,
			Path:    "",
			Handler: o.Info,
		},
		router.Route{
			Method:  router.MGet,
			Path:    "list",
			Handler: o.List,
		},
		router.Route{
			Method:  router.MGet,
			Path:    "badge",
			Handler: o.GetBadge,
		},
	})
}
